package com.hoow.shakoow.shake.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.hoow.shakoow.R
import com.hoow.shakoow.ai.data.RevObjGreetHistory
import com.hoow.shakoow.extend.isValidLink
import com.hoow.shakoow.utils.DateUtil

class GreetHistoryAdapter(private val dataList: List<RevObjGreetHistory>) :
    RecyclerView.Adapter<GreetHistoryAdapter.ViewHolder>() {

    /**
     * 点击回调
     *
     * @return Int 位置
     * @return RevObjAiHomeDemo 值
     * */
    private var mClickActionListener: (Int, RevObjGreetHistory, Boolean) -> Unit =
        { _, _, _ -> }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var imageAdapterAvatar: ImageView =
            view.findViewById(R.id.imageAdapterAvatar)
        var imageAdapterGender: ImageView =
            view.findViewById(R.id.imageAdapterGender)
        var vAdapterActionBg: View =
            view.findViewById(R.id.vAdapterActionBg)
        var textAdapterNickName: TextView = view.findViewById(R.id.textAdapterNickName)
        var textAdapterSignature: TextView = view.findViewById(R.id.textAdapterSignature)
        var textAdapterTimeAgo: TextView = view.findViewById(R.id.textAdapterTimeAgo)
        var textAdapterUnReadCount: TextView = view.findViewById(R.id.textAdapterUnReadCount)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_greet_history, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: RevObjGreetHistory = dataList[position]
        holder.textAdapterNickName.text = data.name
        holder.textAdapterSignature.text = data.lastMsg
        holder.textAdapterTimeAgo.text = DateUtil.getTimeAgo(
            data.createTimeStamp ?: ""
        )//DateUtil.getTimeAgoFromDateString(data.createTime ?: "")

        // 设置图像
        data.headImg?.let {
            if (it.isValidLink()) {
                Glide.with(holder.imageAdapterAvatar)
                    .load(it)
                    .into(holder.imageAdapterAvatar)
            }
        }

        if(data.headImg==null){
            holder.imageAdapterAvatar.setImageResource(R.drawable.icon_placeholder_large)
        }

        // 设置性别
        if (data.sex.equals("0", true)) {
            holder.imageAdapterGender.setImageResource(R.drawable.icon_female)
        } else {
            holder.imageAdapterGender.setImageResource(R.drawable.icon_male)
        }

        // 设置未读消息
        if (data.newMsg <= 0) {
            holder.textAdapterUnReadCount.text = "0"
            holder.textAdapterUnReadCount.visibility = View.GONE
        } else {
            holder.textAdapterUnReadCount.text = data.newMsg.toString()
            holder.textAdapterUnReadCount.visibility = View.VISIBLE
        }

        // 设置回调
        holder.vAdapterActionBg.setOnClickListener {
            mClickActionListener.invoke(position, data, false)
        }

        // 设置回调
        holder.imageAdapterAvatar.setOnClickListener {
            mClickActionListener.invoke(position, data, true)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    /**
     * 设置点击回调
     */
    fun setItemClickListener(listener: (Int, RevObjGreetHistory, Boolean) -> Unit) {
        mClickActionListener = listener
    }
}